package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringBatchInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/3/24.
 */

public class EngineeringBatchInstallationOrderDetaiModel implements IEngineeringBatchInstallationOrderDetaiModel {


    @Override
    public void getEngineeringBatchInstallationOrderDetail(final String orderId, final OnEngineeringBatchInstallationOrderDetailListener engineeringBatchInstallationOrderDetailListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = ProjectUtil.ENGINEERING_BATCH_INSTALLATION_ORDER_DETAIL + "?orderId=" + orderId;
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                HttpResponse response;
                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);


                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != engineeringBatchInstallationOrderDetailListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject dataJsonObj = new JSONObject(json.getString("data"));
                            EngineeringBatchInstallationOrderBean orderBean = new EngineeringBatchInstallationOrderBean();
                            orderBean.setCompanyName(dataJsonObj.getString("companyName"));
                            String completeDateStr = dataJsonObj.getString("completeDate");
                            if (!TextUtils.isEmpty(completeDateStr)) {
                                orderBean.setCompleteDate(Long.parseLong(completeDateStr));
                            }
                            orderBean.setCreateOrderAccountId(dataJsonObj.getString("createOrderAccountId"));
                            JSONArray engineeringDailyFeedbackListJsonArr = dataJsonObj.getJSONArray("engineeringDailyFeedbackList");
                            if (null != engineeringDailyFeedbackListJsonArr) {
                                List<EngineeringDailyFeedBackBean> dailyFeedBackBeenList = new ArrayList<EngineeringDailyFeedBackBean>();

                                for (int i = 0; i < engineeringDailyFeedbackListJsonArr.length(); i++) {
                                    JSONObject engineeringDailyFeedbackListJsonObj = (JSONObject) engineeringDailyFeedbackListJsonArr.get(i);
                                    EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = new EngineeringDailyFeedBackBean();
                                    engineeringDailyFeedBackBean.setAccountId(engineeringDailyFeedbackListJsonObj.getString("accountId"));
                                    engineeringDailyFeedBackBean.setAdvice(engineeringDailyFeedbackListJsonObj.getString("advice"));
                                    engineeringDailyFeedBackBean.setCompleteUnusualAccountId(engineeringDailyFeedbackListJsonObj.getString("completeUnusualAccountId"));
                                    String completeUnusualDateStr = engineeringDailyFeedbackListJsonObj.getString("completeUnusualDate");
                                    if (!TextUtils.isEmpty(completeUnusualDateStr)) {
                                        engineeringDailyFeedBackBean.setCompleteUnusualDate(Long.parseLong(completeUnusualDateStr));
                                    }
                                    engineeringDailyFeedBackBean.setCompleteUnusualDesc(engineeringDailyFeedbackListJsonObj.getString("completeUnusualDesc"));
                                    engineeringDailyFeedBackBean.setConfirmCompleteUnusualAccountId(engineeringDailyFeedbackListJsonObj.getString("confirmCompleteUnusualAccountId"));
                                    String confirmCompleteUnusualDateStr = engineeringDailyFeedbackListJsonObj.getString("confirmCompleteUnusualDate");
                                    if (!TextUtils.isEmpty(confirmCompleteUnusualDateStr)) {
                                        engineeringDailyFeedBackBean.setConfirmCompleteUnusualDate(Long.parseLong(confirmCompleteUnusualDateStr));
                                    }
                                    engineeringDailyFeedBackBean.setContentDesc(engineeringDailyFeedbackListJsonObj.getString("contentDesc"));
                                    String damageNumberStr = engineeringDailyFeedbackListJsonObj.getString("damageNumber");
                                    if (!TextUtils.isEmpty(damageNumberStr)) {
                                        engineeringDailyFeedBackBean.setDamageNumber(Integer.parseInt(damageNumberStr));
                                    }
                                    engineeringDailyFeedBackBean.setDamageProduct(engineeringDailyFeedbackListJsonObj.getString("damageProduct"));
                                    String delayDaysStr = engineeringDailyFeedbackListJsonObj.getString("delayDays");
                                    if (!TextUtils.isEmpty(delayDaysStr)) {
                                        engineeringDailyFeedBackBean.setDelayDays(Integer.parseInt(delayDaysStr));
                                    }

                                    String feedbackDateStr = engineeringDailyFeedbackListJsonObj.getString("feedbackDate");
                                    if (!TextUtils.isEmpty(feedbackDateStr)) {
                                        engineeringDailyFeedBackBean.setFeedbackDate(Long.parseLong(feedbackDateStr));
                                    }
                                    String handleDate = engineeringDailyFeedbackListJsonObj.getString("engineeringDailyFeedbackListJsonObj");
                                    if (!TextUtils.isEmpty(handleDate)) {
                                        engineeringDailyFeedBackBean.setHandleDate(Long.parseLong(handleDate));
                                    }
                                    engineeringDailyFeedBackBean.setHandleInfo(engineeringDailyFeedbackListJsonObj.getString("handleInfo"));
                                    engineeringDailyFeedBackBean.setHandlerAccountId(engineeringDailyFeedbackListJsonObj.getString("handlerAccountId"));
                                    engineeringDailyFeedBackBean.setId(engineeringDailyFeedbackListJsonObj.getString("id"));

                                    JSONArray imgUrlListJsonArr = engineeringDailyFeedbackListJsonObj.getJSONArray("imgUrlList");
                                    if (null != imgUrlListJsonArr) {
                                        List<String> imgUrlList = new ArrayList<String>();
                                        for (int j = 0; j < imgUrlListJsonArr.length(); j++) {
                                            String str = (String) imgUrlListJsonArr.get(j);
                                            imgUrlList.add(str);
                                        }
                                        engineeringDailyFeedBackBean.setImgUrlList(imgUrlList);
                                    }


                                    engineeringDailyFeedBackBean.setOmitContent(engineeringDailyFeedbackListJsonObj.getString("omitContent"));
                                    String omitNumberStr = engineeringDailyFeedbackListJsonObj.getString("omitNumber");
                                    if (!TextUtils.isEmpty(omitNumberStr)) {
                                        engineeringDailyFeedBackBean.setOmitNumber(Integer.parseInt(omitNumberStr));
                                    }

                                    JSONArray otherImgUrlListJsonArr = engineeringDailyFeedbackListJsonObj.getJSONArray("otherImgUrlList");
                                    if (null != otherImgUrlListJsonArr) {
                                        List<String> imgUrlList = new ArrayList<String>();
                                        for (int j = 0; j < otherImgUrlListJsonArr.length(); j++) {
                                            String str = (String) otherImgUrlListJsonArr.get(j);
                                            imgUrlList.add(str);
                                        }
                                        engineeringDailyFeedBackBean.setOtherImgUrlList(imgUrlList);
                                    }


                                    engineeringDailyFeedBackBean.setStatus(engineeringDailyFeedbackListJsonObj.getString("status"));
                                    String subTypeStr = engineeringDailyFeedbackListJsonObj.getString("subType");
                                    if (!TextUtils.isEmpty(subTypeStr)) {
                                        if (subTypeStr.equals("DAMAGE")) {
                                            engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.DAMAGE);
                                        } else if (subTypeStr.equals("INSTALL_UNUSUAL")) {
                                            engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.INSTALL_UNUSUAL);
                                        } else if (subTypeStr.equals("OMIT")) {
                                            engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.OMIT);
                                        }

                                    }
                                    String typeStr = engineeringDailyFeedbackListJsonObj.getString("type");
                                    if (!TextUtils.isEmpty(typeStr)) {
                                        if (typeStr.equals("NORMAL")) {
                                            engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.NORMAL);
                                        } else if (typeStr.equals("UNUSUAL")) {
                                            engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.UNUSUAL);
                                        } else if (typeStr.equals("COMPLETED")) {
                                            engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.COMPLETED);
                                        }

                                    }
                                    String unusualDaysStr = engineeringDailyFeedbackListJsonObj.getString("unusualDays");
                                    if (!TextUtils.isEmpty(unusualDaysStr)) {
                                        engineeringDailyFeedBackBean.setUnusualDays(Integer.parseInt(unusualDaysStr));
                                    }
                                    engineeringDailyFeedBackBean.setUnusualReason(engineeringDailyFeedbackListJsonObj.getString("unusualReason"));
                                    String unusualStatusStr = engineeringDailyFeedbackListJsonObj.getString("type");
                                    if (!TextUtils.isEmpty(unusualStatusStr)) {
                                        if (unusualStatusStr.equals("WAIT_CONFIRM")) {
                                            engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_CONFIRM);
                                        } else if (unusualStatusStr.equals("WAIT_REPLY")) {
                                            engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_REPLY);
                                        } else if (unusualStatusStr.equals("WAIT_REPLY_CONFIRM")) {
                                            engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM);
                                        } else if (unusualStatusStr.equals("REPLY_CONFIRM_FINISH")) {
                                            engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH);
                                        }

                                    }

                                    dailyFeedBackBeenList.add(engineeringDailyFeedBackBean);

                                }
                                orderBean.setEngineeringDailyFeedbackList(dailyFeedBackBeenList);


                            }

                            orderBean.setHasPauseOrder(dataJsonObj.getBoolean("hasPauseOrder"));
                            orderBean.setId(dataJsonObj.getString("id"));
                            orderBean.setProjectName(dataJsonObj.getString("projectName"));
                            orderBean.setReceiveAccountId(dataJsonObj.getString("receiveAccountId"));
                            String receiveOrderTimeStr = dataJsonObj.getString("receiveOrderTime");
                            if (!TextUtils.isEmpty(receiveOrderTimeStr)) {
                                orderBean.setReceiveOrderTime(Long.parseLong(receiveOrderTimeStr));
                            }
                            String sendOrderTimeStr = dataJsonObj.getString("sendOrderTime");
                            if (!TextUtils.isEmpty(sendOrderTimeStr)) {
                                orderBean.setSendOrderTime(Long.parseLong(sendOrderTimeStr));
                            }

                            String status = dataJsonObj.getString("status");
                            if (null != status) {
                                if (status.equals("CREATED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CREATED);
                                } else if (status.equals("SEND")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.SEND);

                                } else if (status.equals("RECEIVED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RECEIVED);
                                } else if (status.equals("RETURNED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RETURNED);
                                }
//                                else if (status.equals("CONSTRUCT")) {
//                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CONSTRUCT);
//                                } else if (status.equals("FINISH")) {
//                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.FINISH);
//                                } else if (status.equals("PAUSE")) {
//                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.PAUSE);
//                                }
                            }
                            engineeringBatchInstallationOrderDetailListener.getDataSuccessed(orderBean);

                        } else {
                            engineeringBatchInstallationOrderDetailListener.getDataFailed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnEngineeringBatchInstallationOrderDetailListener {
        void getDataSuccessed(EngineeringBatchInstallationOrderBean engineeringBatchInstallationOrderBean);

        void getDataFailed(String desc);
    }

}
